import { login, getUserInfo, getUserDetail } from '@/api/user'
// 在根模块里使用数据持久化，就不需要手动存储本地了
// import { setToken as setTokenLocal, getToken as getTokenLocal } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    // token: getTokenLocal(),
    token: null,
    userInfo: {},
    timeStamp: ''
  },
  mutations: {
    setToken(state, payload) {
      state.token = payload
      state.timeStamp = +new Date()
      // setTokenLocal(payload)
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    async loginAction(context, payload) {
      const data = await login(payload)
      // console.log(data)
      context.commit('setToken', data)
    },
    async getUserInfo(context) {
      const res = await getUserInfo()
      // console.log(res)
      const detail = await getUserDetail(res.userId)
      // console.log(detail)
      const userInfo = { ...res, ...detail }
      context.commit('setUserInfo', userInfo)
    },
    logout(context) {
      context.commit('setToken', null)
      context.commit('setUserInfo', {})
    }
  }

}
